﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Control.Bus;

namespace Control.Dal
{
    class DalImportDetail
    {
        internal static int Insert(Control.Bus.BusImportBillDetail busImportBillDetail)
        {
            ImportBillDetail BillDetail = new ImportBillDetail()
            {
                IDBill = busImportBillDetail.Bill.ID,
                IDProd = busImportBillDetail.Prod.ID,
                Quantity = busImportBillDetail.Quantity
            };
            DalCommon.Database.ImportBillDetails.InsertOnSubmit(BillDetail);
            DalCommon.Database.SubmitChanges();
            return BillDetail.ID;
        }

        internal static void Delete(Control.Bus.BusImportBillDetail busImportBillDetail)
        {
            WareHouseDataContext db = DalCommon.Database;
            try
            {
                ImportBillDetail BillDetail = db.ImportBillDetails.Single(b => b.ID == busImportBillDetail.ID);
                db.ImportBillDetails.DeleteOnSubmit(BillDetail);
                db.SubmitChanges();
            }
            catch { }
        }

        internal static List<Control.Bus.BusImportBillDetail> GetAllDetailByBill(Control.Bus.BusImportBill busImportBill)
        {
            var BillDetails = from b in DalCommon.Database.ImportBillDetails
                              where b.IDBill == busImportBill.ID
                              select new BusImportBillDetail()
                              {
                                  ID = b.ID,
                                  Bill = new BusImportBill()
                                  {
                                      ID = b.ImportBill.ID,
                                      Date = (DateTime)b.ImportBill.Date,
                                      Staff = new BusStaff()
                                      {
                                          ID=b.ImportBill.Staff.ID,
                                          PassWord=b.ImportBill.Staff.PassWord,
                                          UserName=b.ImportBill.Staff.UserName
                                      }
                                  },
                                  Prod = BusProduct.GetProdByCode(b.Product.ProdCode),
                                  Quantity = (int)b.Quantity,
                                  IsSaved=true,
                              };
            return BillDetails.ToList<BusImportBillDetail>();
        }

        internal static BusImportBillDetail GetByID(int p)
        {
            var BillDetails = from b in DalCommon.Database.ImportBillDetails
                              where b.ID==p
                              select new BusImportBillDetail()
                              {
                                  ID = b.ID,
                                  Bill = new BusImportBill()
                                  {
                                      ID = b.ImportBill.ID,
                                      Date = (DateTime)b.ImportBill.Date,
                                      Staff = new BusStaff()
                                      {
                                          ID = b.ImportBill.Staff.ID,
                                          PassWord = b.ImportBill.Staff.PassWord,
                                          UserName = b.ImportBill.Staff.UserName
                                      }
                                  },
                                  Prod = BusProduct.GetProdByCode(b.Product.ProdCode),
                                  Quantity = (int)b.Quantity
                              };
            return BillDetails.FirstOrDefault<BusImportBillDetail>();
        }

        internal static void UpDate(BusImportBillDetail busImportBillDetail)
        {
            WareHouseDataContext db = DalCommon.Database;
            ImportBillDetail billdetail = db.ImportBillDetails.Single(bd => bd.ID == busImportBillDetail.ID);
            billdetail.IDBill = busImportBillDetail.Bill.ID;
            billdetail.IDProd = busImportBillDetail.Prod.ID;
            billdetail.Quantity = busImportBillDetail.Quantity;
            db.SubmitChanges();
        }

        internal static List<BusImportBillDetail> Find(string ProdCode, DateTime newfromDate, DateTime newtoDate)
        {
            var BillDetails = from b in DalCommon.Database.ImportBillDetails
                              where b.Product.ProdCode.Contains(ProdCode)&&b.ImportBill.Date.CompareTo(newfromDate)>=0&&b.ImportBill.Date.CompareTo(newtoDate)<=0
                              select new BusImportBillDetail()
                              {
                                  ID = b.ID,
                                  Bill = new BusImportBill()
                                  {
                                      ID = b.ImportBill.ID,
                                      Date = (DateTime)b.ImportBill.Date,
                                      Staff = new BusStaff()
                                      {
                                          ID = b.ImportBill.Staff.ID,
                                          PassWord = b.ImportBill.Staff.PassWord,
                                          UserName = b.ImportBill.Staff.UserName
                                      }
                                  },
                                  Prod = BusProduct.GetProdByCode(b.Product.ProdCode),
                                  Quantity = (int)b.Quantity,
                                  IsSaved = true,
                              };
            return BillDetails.ToList<BusImportBillDetail>();
        }
    }
}
